//
//  QuestionGroup.swift
//  RabbleWabble
//
//  Created by TRS-Mobile on 2021/9/7.
//

import Foundation

public struct QuestionGroup: Codable {
    public let questions: [Question]
    public private(set) var score: Score
    public let title: String
    
    init(questions: [Question],
         title: String,
         score: Score = Score()) {
        self.questions = questions
        self.score = score
        self.title = title
    }
}

public class Score: Codable {
    public var correctCount: Int = 0 {
        didSet {
            updateRunningPercentage()
        }
    }
    public var incorrectCount: Int = 0 {
        didSet {
            updateRunningPercentage()
        }
    }
    
    func reset() {
        correctCount = 0
        incorrectCount = 0
    }
    
    private func caculateRunningPercentage() -> Double {
        let totalCount = correctCount + incorrectCount
        guard totalCount > 0 else {
            return 0
        }
        return Double(correctCount) / Double(totalCount)
    }
    
    private func updateRunningPercentage() {
        runningPercentage.value = caculateRunningPercentage()
    }
    
    lazy var runningPercentage = Observable(caculateRunningPercentage())
}
